package com.sykj.springboot.Redis;

import com.sykj.springboot.common.utils.RedisUtils;
import com.sykj.springboot.entity.SysUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class test {

    //由于之前已经自定义注入RedisTemplate组件，因而在此可以直接自动装配
    @Autowired
    private RedisTemplate redisTemplate;
    //采用RedisTemplate将字符串信息写入缓存中并读取出来
    @Test
    public void one(){
        System.out.println("------开始RedisTemplate操作组件实战----");
        //定义字符串内容及存入缓存的key
        final String content="RedisTemplate实战字符串信息";
        final String key="redis:template:one:string";
        //Redis通用的操作组件
        ValueOperations valueOperations=redisTemplate.opsForValue();
        //将字符串信息写入缓存中
        System.out.println("写入缓存中的内容：{} "+ content);
        valueOperations.set(key, content);
        //从缓存中读取内容
        Object result=valueOperations.get(key);
        System.out.println("读取出来的内容：{} "+result);
    }


    @Test
    public void one2(){
        RedisUtils.addStringToRedis("token","ssssssssss");

        String token = RedisUtils.getStringFromRedis("token");
        System.out.println(token);

        SysUser sysUser = new SysUser();
        RedisUtils.addObjToRedis("user",sysUser);

        String user = RedisUtils.getStringFromRedis("user");
        System.out.println(user);

        SysUser user2 = (SysUser)RedisUtils.getObjFromRedis("user");
        System.out.println(user2);

    }

}
